package com.water.expert.extraction.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TokenController {

    @Autowired
    private OAuth2AuthorizedClientService authorizedClientService;

    @GetMapping("/token")
    public String getAccessToken(@AuthenticationPrincipal OAuth2User principal) {
        OAuth2AuthorizedClient authorizedClient =
            authorizedClientService.loadAuthorizedClient("authserver", principal.getName());
        return authorizedClient.getAccessToken().getTokenValue(); // ✅ 返回 OAuth2 `access_token`
    }
}
